/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.aseclipse.osgi.loader
{
	import mx.core.Application;
	 
    public class BundleConstants
    {
        public static const STARTUP_BUNDLE:String = "org.aseclipse.ui";
        public static const ROOT_CONFIG:String = "osgi/config.ini";        
        /******
        * Access URL pattern for OSGi resource, like "osgi/${bundle.name}/${file.name}"
        */ 
        public static var osgiAccessURL:String = "osgi/${bundle.name}/${file.name}";
        public static const PATTERN_PLUGIN:String = "1";
        /******
        * Access URL pattern for extender plugins, like "osgi/extenders/${bundle.name}"
        */ 
        public static var osgiExtendersURL:String = "osgi/extenders/${bundle.name}";
        public static const PATTERN_EXTENDER:String = "2";
        /******
        * Access URL pattern for extender plugins, like "osgi/requires/${bundle.name}"
        */ 
        public static var osgiRequiresURL:String = "osgi/requires/${bundle.name}";
        public static const PATTERN_REQUIRES:String = "3";
                
        public static const OSGI_URL_BUNDLE_RESOURCE_KEY:String = "osgi.url.bundle.resource";
        public static const OSGI_URL_BUNDLE_EXTENDER_KEY:String = "osgi.url.bundle.extenders";
        public static const OSGI_BUNDLES_KEY:String = "osgi.bundles";        
        public static const BUNDLE_NAME_HOLDER:String = "${bundle.name}";
        public static const RESOURCE_NAME_HOLDER:String = "${file.name}";
    
        public static function getResourceURL(bundlename:String, configFile:String):String {
            if(osgiAccessURL) {
                var url:String = new String(osgiAccessURL);
                url = url.replace(BUNDLE_NAME_HOLDER, bundlename);
                url = url.replace(RESOURCE_NAME_HOLDER, configFile);
                return url;
            }
            return null;
        }
        
        public static function getExtendersURL(bundlename:String):String {
            if(osgiExtendersURL) {
                var url:String = new String(osgiExtendersURL);
                url = url.replace(BUNDLE_NAME_HOLDER, bundlename);
                return url;
            }
            return null;
        }
        
        public static function enrichAcessingPort(map:Object):void  {
            osgiAccessURL = map[OSGI_URL_BUNDLE_RESOURCE_KEY] as String;
            osgiExtendersURL = map[OSGI_URL_BUNDLE_EXTENDER_KEY] as String;
        }
        /***
        * return current url prefix like "http://localhost:8080/context/" if url is 
        * "http://localhost:8080/context/Main.swf" or
        * "http://localhost:8080/context/osgi/Main.swf"
        */ 
        public static function getURLPrefix():String {
        	var url:String = Application.application.url + "/";
        	var slash:int = url.indexOf("//");
        	slash = url.indexOf("/", slash + 2);
        	slash = url.indexOf("/", slash + 1);
        	return url.substring(0, slash + 1);
        }        
        
        public static function updateSubfix(input:String):String {
            return input;
        }
    }
}
